package pl.ncdc.web.login.server.service;

import java.io.File;

import pl.ncdc.web.login.client.service.AuthenticationResult;
import pl.ncdc.web.login.client.service.AuthenticationService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Demo version of {@link AuthenticationService}.
 * <p>
 * Created on Apr 17, 2008
 *
 * @author hshsce
 * @version $Id: DemoAuthenticationService.java 43 2008-04-19 10:26:10Z hshsce $
 */
public class DemoAuthenticationService extends RemoteServiceServlet implements AuthenticationService {

	private final File m_userImagesDir = new File("src/demo/pl/ncdc/web/login/public/images/users");

	public AuthenticationResult login(final String uid, final String password, final boolean rememberMe) {
		final int code;
		if ((new File(m_userImagesDir, uid + ".jpg")).exists()) {
			if ((uid != null) && uid.equals(password)) {
				code = AuthenticationResult.OK;
				getThreadLocalRequest().getSession().setAttribute("AutenticationStatus", AuthenticationResult.OK);
			} else {
				code = AuthenticationResult.INVALID_PASSWORD;
			} 
		} else {
			code = AuthenticationResult.INVALID_UID; 
		}
		if (code != AuthenticationResult.OK) {
			try {
				Thread.sleep(1000);
			} catch (final InterruptedException e) {
				//nothing to do
			}			
		}
		
		return new AuthenticationResult(code) ;
	}

	public void logout() {
		getThreadLocalRequest().getSession().invalidate();
	}

}
